﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using cwtrock.service.impl;
using cwtrock.model;

namespace cwtrock.view.sysmng
{
    public partial class VouKeywordFrm : Form
    {
        private SysVouKeywordServiceImpl service = new SysVouKeywordServiceImpl();
        private List<SysVouKeywork> list;
        private static VouKeywordFrm frm = null;


        #region 单体模式
        /// <summary>
        /// 单体模式
        /// </summary>
        /// <returns></returns>
        public static VouKeywordFrm getInstance()
        {
            if (frm == null)
            {
                frm = new VouKeywordFrm();

            }

            return frm;
        }
        #endregion

        public VouKeywordFrm()
        {
            InitializeComponent();
        }

        private void addBtn_Click(object sender, EventArgs e)
        {
            VouKeywordAdd frm = new VouKeywordAdd(this, 0);
            frm.Show();
        }

        private void modBtn_Click(object sender, EventArgs e)
        {
            if (this.dataGridView1.SelectedRows.Count == 0)
            {
                MessageBox.Show("请选择一条记录进行修改", "系统提示", MessageBoxButtons.OK);
            }
            else
            {
                int idx = this.dataGridView1.SelectedRows[0].Index;
                int id = Convert.ToInt32(this.dataGridView1.Rows[idx].Cells[1].Value.ToString());
                VouKeywordAdd frm = new VouKeywordAdd(this, id);
                frm.Show();
            }
           
        }

        private void delBtn_Click(object sender, EventArgs e)
        {
            if (this.dataGridView1.SelectedRows.Count == 0)
            {
                MessageBox.Show("请选择一条记录", "系统提示", MessageBoxButtons.OK);
            }
            else
            {
                int idx = this.dataGridView1.SelectedRows[0].Index;
                int id = Convert.ToInt32(this.dataGridView1.Rows[idx].Cells[1].Value.ToString());
                service.delete(id);
                refresh();
            }
        }

        public void refresh()
        {
            list = service.findAll();
            this.dataGridView1.DataSource = list;
        }


        private void SolverFrm_Load(object sender, EventArgs e)
        {
            this.dataGridView1.AutoGenerateColumns = false;
            this.dataGridView1.AutoSize = false;
            list = service.findAll();
            this.dataGridView1.DataSource = list;

            DataGridViewColumn column = new DataGridViewTextBoxColumn();
            column.DataPropertyName = "KEYWORD";
            column.Name = "code";
            column.HeaderText = "代码";
            this.dataGridView1.Columns.Add(column);

            column = new DataGridViewTextBoxColumn();
            column.Visible = false;
            column.DataPropertyName = "ID";
            column.Name = "id";
            column.HeaderText = "ID";
            this.dataGridView1.Columns.Add(column);
        }
    }
}
